import os
import json
import subprocess
import shutil

def read_json_file(json_file):
    if not os.path.exists(json_file):
        return []
    with open(json_file, 'r') as f:
        ret = json.load(f)
    return ret

def write_json_file(json_file, content):
    path = os.path.dirname(json_file)
    if not os.path.exists(path):
        os.makedirs(path)
    with open(json_file, 'w') as f:
        json.dump(content, f, indent=4)

filename = 'compile_commands.json'

YSYX_HOME = os.environ['YSYX_HOME']
# NEMU
NEMU_HOME = os.path.join(YSYX_HOME, "nemu")
# NPC
NPC_HOME = os.path.join(YSYX_HOME, "npc")
# AM
AM_HOME = os.path.join(YSYX_HOME, "abstract-machine", "am")
KLIB_HOME = os.path.join(YSYX_HOME, "abstract-machine", "klib")
# NACY-APPS
NAVY_HOME = os.path.join(YSYX_HOME, "navy-apps")
LIBC_HOME = os.path.join(NAVY_HOME, "libs", "libc")
LIBOS_HOME = os.path.join(NAVY_HOME, "libs", "libos")
LIBNDL_HOME = os.path.join(NAVY_HOME, "libs", "libndl")
LIBFIXEDPTC_HOME = os.path.join(NAVY_HOME, "libs", "libfixedptc")
LIBMINISDL_HOME = os.path.join(NAVY_HOME, "libs", "libminiSDL")
# NANOS-LITE
NANOS_HOME = os.path.abspath(os.path.join(YSYX_HOME, "nanos-lite"))

# NEMU
subprocess.Popen("compiledb -n make", shell=True, cwd=NEMU_HOME).wait()
if os.path.exists(os.path.join(NEMU_HOME, filename)):
    print("NEMU done")
# NPC
subprocess.Popen("make", shell=True, cwd=NPC_HOME, stdout=subprocess.DEVNULL).wait()
obj_dir = os.path.join(NPC_HOME, "build", "obj_dir")
subprocess.Popen(f"compiledb -n make -f Vtop.mk", shell=True, cwd=obj_dir).wait()
shutil.move(os.path.join(obj_dir, filename), os.path.join(NPC_HOME, filename))
if os.path.exists(os.path.join(NPC_HOME, filename)):
    print("NPC done")
# AM
subprocess.Popen("compiledb -n make ARCH=riscv64-nemu archive", shell=True, cwd=AM_HOME).wait()
if os.path.exists(os.path.join(AM_HOME, filename)):
    print("AM done")
subprocess.Popen("compiledb -n make ARCH=riscv64-nemu archive", shell=True, cwd=KLIB_HOME).wait()
if os.path.exists(os.path.join(AM_HOME, filename)):
    print("KLIB done")

# NAVY-APPS
subprocess.Popen("compiledb -n make ISA=riscv64", shell=True, cwd=LIBC_HOME).wait()
if os.path.exists(os.path.join(LIBC_HOME, filename)):
    print("LIBC done")
subprocess.Popen("compiledb -n make ISA=riscv64", shell=True, cwd=LIBOS_HOME).wait()
if os.path.exists(os.path.join(LIBOS_HOME, filename)):
    print("LIBOS done")
subprocess.Popen("compiledb -n make ISA=riscv64", shell=True, cwd=LIBNDL_HOME).wait()
if os.path.exists(os.path.join(LIBNDL_HOME, filename)):
    print("LIBNDL done")
subprocess.Popen("compiledb -n make ISA=riscv64", shell=True, cwd=LIBFIXEDPTC_HOME).wait()
if os.path.exists(os.path.join(LIBFIXEDPTC_HOME, filename)):
    print("LIBFIXEDPTC done")
subprocess.Popen("compiledb -n make ISA=riscv64", shell=True, cwd=LIBMINISDL_HOME).wait()
if os.path.exists(os.path.join(LIBMINISDL_HOME, filename)):
    print("LIBMINISDL done")

# Nanos-Lite
subprocess.Popen("compiledb -n make ARCH=riscv64-nemu", shell=True, cwd=NANOS_HOME).wait()
if os.path.exists(os.path.join(NANOS_HOME, filename)):
    print("NANOS-LITE done")
